/*

  Copyright (C) 2013 xubinbin 徐彬彬 (Beijing China)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  
  filename: fa_ts2es.h 
  version : v1.1.0
  time    : 2013/08/02 16:20 
  author  : xubinbin ( xubbwd@gmail.com )
  code URL: http://code.google.com/p/tsdemux/
  
*/

#ifndef	__FA_TS2ES_H__
#define __FA_TS2ES_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define PS_INPUT_SIZE	 (4096)
#define PS_MAX_BUF	     (500*1024)

typedef struct PS_pack_header
{
    unsigned pack_start_code                       : 32;
    unsigned system_clock_reference_base_32_30     : 3;
    unsigned system_clock_reference_base_29_15     : 15;
    unsigned system_clock_reference_base_14_0      : 15;
    unsigned system_clock_reference_extension      : 9;
    unsigned program_mux_rate                      : 22;
    unsigned pack_stuffing_length                  : 3;
}PS_pack_header;

typedef struct PS_system_header
{
    unsigned system_header_start_code              : 32;
    unsigned header_length                         : 16;
    unsigned rate_bound                            : 22;
    unsigned audio_bound                           : 6;
    unsigned fixed_flag                            : 1;
    unsigned CSPS_flag                             : 1;
    unsigned system_audio_lock_flag                : 1;
    unsigned system_video_lock_flag                : 1;
    unsigned video_bound                           : 5;
    unsigned packet_rate_restriction_flag          : 1;
    unsigned stream_id                             : 8;
    unsigned P_STD_buffer_bound_scale              : 1;
    unsigned P_STD_buffer_size_bound               : 3;
}PS_system_header;

typedef struct PS_PES
{
    unsigned packet_start_code_prefix                : 24;
    unsigned stream_id                               : 8;
    unsigned PES_packet_length                       : 16;
    unsigned PES_scrambling_control                  : 2;
    unsigned PES_priority                            : 1;
    unsigned data_alignment_indicator                : 1;
    unsigned copyright                              : 1;
    unsigned original_or_copy                       : 1;
    unsigned PTS_DTS_flags                          : 2;
    unsigned ESCR_flag                              : 1;
    unsigned ES_rate_flag                           : 1;
    unsigned DSM_trick_mode_flag                    : 1;
    unsigned additional_copy_info_flag              : 1;
    unsigned PES_CRC_flag                           : 1;
    unsigned PES_extension_flag                     : 1;
    unsigned PES_header_data_length                 : 8;
    unsigned PTS_marker_bit                         : 1;
    unsigned PTS_32_30                              : 3;
    unsigned PTS_29_15                              : 15;
    unsigned PTS_14_0                               : 15;
} PS_PES;

typedef struct psdemux
{
    int pes_num;
	unsigned char * buffer;
	unsigned char * tmp_buffer;     //memcpy addr
	long bufSize;
	long bufLen;                    //active length
    unsigned long es_Data_Len;      //output data
    unsigned long cur_buf_Addr;     //cursor addr
    int find_ps_header_flag;
    int readin_data_flag;           //
    int error_pes_packer;           // not video pes
    PS_pack_header     PS_pack_header_info;
    PS_system_header   PS_system_header_info;
    PS_PES             PS_pes_info;
}psdemux;


extern psdemux *psdemux_info;
extern int psdemux_init(psdemux *psdemux_info);
extern int psdemux_get_es_process(psdemux *psdemux_info,unsigned char * input_buffer,int input_length,unsigned char * es_data,unsigned int *es_data_length);
extern int psdemux_deinit(psdemux *psdemux_info);


 #endif

